knitr::opts_chunk$set(echo = TRUE)
#I load my packages as I use them so you can see what 
#they are used with.
library(dplyr)
library(tidycensus)
library(ggplot2)
library(mapdeck)
library(shiny)
library(shinythemes)
library(shinyjs)
library(ggplot2)
library(maps)
library(plotly)
library(DT)
library(dplyr)
library(tigris)
library(tidyverse)
library(tidycensus)
library(readxl)
library(collapsibleTree)
library(shinycssloaders)
library(leaflet)
library(leaflet.extras)
library(rvest)
library(sf)
library(shinydashboard)
library(shinydashboardPlus)
library(tidygeocoder)
library(janitor)
library(dplyr)
library(ggplot2)
library(magrittr)
options(tigris_use_cache = TRUE)
readRenviron("~/.Renviron")
Sys.getenv("CENSUS_API_KEY")
## [1] "0a48e15de56bd55aa9f498f66dfe9c43193b922b"
blocks<-c("Block Group 1, Census Tract 6112.05, Loudoun County, Virginia",
"Block Group 2, Census Tract 6112.05, Loudoun County, Virginia",
"Block Group 3, Census Tract 6112.05, Loudoun County, Virginia",
"Block Group 2, Census Tract 6112.04, Loudoun County, Virginia",
"Block Group 3, Census Tract 6112.04, Loudoun County, Virginia",
"Block Group 2, Census Tract 6115.02, Loudoun County, Virginia",
"Block Group 3, Census Tract 6115.02, Loudoun County, Virginia",
"Block Group 1, Census Tract 6113, Loudoun County, Virginia",
"Block Group 2, Census Tract 6113, Loudoun County, Virginia",
"Block Group 3, Census Tract 6113, Loudoun County, Virginia",
"Block Group 1, Census Tract 6114, Loudoun County, Virginia",
"Block Group 2, Census Tract 6114, Loudoun County, Virginia",
"Block Group 3, Census Tract 6114, Loudoun County, Virginia",
"Block Group 1, Census Tract 6117.01, Loudoun County, Virginia",
"Block Group 2, Census Tract 6117.01, Loudoun County, Virginia",
"Block Group 1, Census Tract 6116.02, Loudoun County, Virginia",
"Block Group 2, Census Tract 6116.02, Loudoun County, Virginia",
"Block Group 1, Census Tract 6116.01, Loudoun County, Virginia",
"Block Group 2, Census Tract 6116.01, Loudoun County, Virginia", 
"Block Group 3, Census Tract 6112.02, Loudoun County, Virginia")

va20_2 <- get_acs(geography = "block group",
                variables = c(hispanic = "B03002_012"),
                state = "VA",
                year = 2020,
                geometry = TRUE) %>%
filter(NAME %in% blocks)
## Getting data from the 2016-2020 5-year ACS
blocks_CDP<-c("Block Group 2, Census Tract 6115.02, Loudoun County, Virginia","Block Group 3, Census Tract 6115.02, Loudoun County, Virginia", "Block Group 1, Census Tract 6113, Loudoun County, Virginia","Block Group 2, Census Tract 6113, Loudoun County, Virginia","Block Group 3, Census Tract 6113, Loudoun County, Virginia", "Block Group 1, Census Tract 6114, Loudoun County, Virginia","Block Group 2, Census Tract 6114, Loudoun County, Virginia","Block Group 3, Census Tract 6114, Loudoun County, Virginia","Block Group 1, Census Tract 6117.01, Loudoun County, Virginia","Block Group 2, Census Tract 6117.01, Loudoun County, Virginia", "Block Group 1, Census Tract 6116.02, Loudoun County, Virginia","Block Group 2, Census Tract 6116.02, Loudoun County, Virginia","Block Group 1, Census Tract 6116.01, Loudoun County, Virginia","Block Group 2, Census Tract 6116.01, Loudoun County, Virginia")

va_20_CDP <- va20_2 %>% filter (NAME %in% blocks_CDP)
zap2 <- ggplot(va20_2$geometry) + geom_sf(aes(fill=va20_2$estimate)) +
scale_color_viridis_c() + scale_fill_viridis_c()+
coord_sf(datum = NA) +
theme_minimal() +labs(fill = "Hispanic Population")

zap2

library(leaflet)
mypal <- colorNumeric(
palette = "magma",
domain = va20_2$estimate)
mypal(c(10, 20, 30, 40, 50))
## Warning in mypal(c(10, 20, 30, 40, 50)): Some values were outside the color
## scale and will be treated as NA
## [1] "#808080" "#808080" "#808080" "#808080" "#808080"
leaflet() %>%
addProviderTiles(providers$Stamen.TonerLite) %>%
addPolygons(data = va20_2,
color = ~mypal(estimate),
weight = 0.5,
smoothFactor = 0.2,
fillOpacity = 0.5,
label = ~estimate) %>%
addLegend(
position = "bottomright",
pal = mypal,
values = va20_2$estimate,
title = "Hispanic Population"
)
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
map <- read_excel("/Users/nandinidas/Desktop/2022_DSPG_Loudoun/data/school_locations.xlsx")

map$Longitude <- as.numeric(map$Longitude)
map$Latitude <- as.numeric(map$Latitude)

total <- merge(va20_2,map)

#specify the bin breaks
mybins <- c(0,300,600,900,1200,1500,1800,2100)
#specify the default color
mypalette <- colorBin(palette="inferno", domain=va20_2$estimate, na.color="transparent", bins=mybins)

leaflet(data = total) %>%
  addTiles() %>%
  addPolygons(
    fillColor = ~mypalette(va20_2$`estimate`),
    stroke=TRUE,
    weight = 1,
    smoothFactor = 0.2,
    opacity = 1.0,
    fillOpacity = 0.7, 
    label=paste("County: ",va20_2$GEOID, ", Value: ",va20_2$estimate),
    highlightOptions = highlightOptions(color = "white",
                                        weight = 2,
                                        bringToFront = TRUE)) %>%
  addLegend(pal=mypalette, position = "bottomright",
            values = ~va20_2$estimate,
            opacity = 0.5, title = "Hispanic Population") %>%
  addMarkers( ~Longitude, ~Latitude, popup = ~as.character(Address), label = ~as.character(School), labelOptions = TRUE)
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
#specify the bin breaks
#mybins <- c(0,300,600,900,1200,1500,1800,2100)
#specify the default color
#mypalette <- colorBin(palette="inferno", domain=va20_2$estimate, na.color="transparent", bins=mybins)
require(rgdal)
## Loading required package: rgdal
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.2, released 2022/03/08
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: FALSE 
## Loaded PROJ runtime: Rel. 8.2.1, January 1st, 2022, [PJ_VERSION: 821]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-7
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
sugarland <- st_read(dsn = "/Users/nandinidas/Desktop/2022_DSPG_Loudoun/Sugarland_Elementary_AZ", "SugarL_ES_AZ", stringsAsFactors = FALSE)
## Reading layer `SugarL_ES_AZ' from data source 
##   `/Users/nandinidas/Desktop/2022_DSPG_Loudoun/Sugarland_Elementary_AZ' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 1 field
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -8615210 ymin: 4725082 xmax: -8612912 ymax: 4728103
## Projected CRS: WGS 84 / Pseudo-Mercator
Sugar <- as_Spatial(sugarland)
va20S <- as_Spatial(va20_2)

leaflet(data = Sugar) %>%
 fitBounds(lng1 = -77.458921, lat1 = 39.000, lng2 = -77.483035, lat2=39.1) %>%
  addTiles() %>%
  addPolygons(
    fillColor = "red",
    stroke=TRUE,
    weight = 1,
    smoothFactor = 0.2,
    opacity = 1.0,
    fillOpacity = 0.7,
    color="red",
    highlightOptions = highlightOptions(color = "white",
                                        weight = 2,
                                        bringToFront = TRUE))
plot(Sugar, col = "lightgreen")

#plot(va20S, col = "blue", add = TRUE, plot.new())

School wise - Gender

library(plotly)
df2 <- data.frame(sex=rep(c("Male", "Female"), each=6),
                schools=c("Sugarland","Rolling Ridge","Guilford","Sterling Elementary","Sully","Forest Grove"),
                number=c(268, 273, 278, 237,221, 282, 255, 259, 272, 200, 217, 278))

p<- ggplot(data=df2, aes(x=schools, y=number, fill=sex,  width=0.9)) +
  geom_bar(stat="identity", position="stack") +
  scale_fill_manual(values = c("#E69F00", "#56B4E9")) + labs(y="", x="", fill="")+ggtitle("Gender by Schools") + theme_minimal() + 
  geom_text(aes(label = number, y = number), size = 3, position = position_stack(vjust = 0.5))
ggplotly(p)

#{r} #ggplot(df2, aes(fill = sex, y=number, x=schools)) + #geom_bar(position="dodge", stat="identity") + #scale_fill_viridis(discrete = T, option = "E") + #ggtitle("Studying 4 species..") + #facet_wrap(~schools) + #theme_ipsum() + #theme(legend.position="none") + #xlab("") #

library(plotly)

df2 <- data.frame(sex=rep(c("Missed less than 10%", "Missed 10% or more"), each=6),
                schools=c("Sugarland","Rolling Ridge","Guilford","Sterling Elementary","Sully","Forest Grove"),
                number=c(11.1, 10.1, 6.7, 5.8, 9.7,7.9,88.9,89.9,93.3,94.2,90.3,92.1))

q<- ggplot(data=df2, aes(x=schools, y=number, fill=sex,  width=0.8)) +
  geom_bar(stat="identity") +
  scale_fill_manual(values = c("red", "#56B4E9")) + labs(y="", x="", fill="")+ggtitle("Percentage of Chronic absenteeism") + theme(axis.text.x = element_text(angle = 45)) +
  geom_text(aes(label = number, y = number), size = 3, position = position_stack(vjust = 0.5)) 

ggplotly(q)